package com.zllkj.mm.module.goods

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.common.L
import com.zllkj.mm.databinding.ActivityOrderDetailBinding
import com.zllkj.mm.databinding.FragmentAfterSaleDetailBinding
import com.zllkj.mm.databinding.FragmentGoodsDetailBinding
import com.zllkj.mm.model.entity.*
import com.zllkj.mm.module.goods.vm.GoodsDetailFragmentVM
import com.zllkj.mm.module.shopping.fragment.OrderConfirmFragment
import com.zllkj.mm.module.shopping.fragment.ShoppingCardFragment
import com.zllkj.mm.module.shopping.vm.ItemShoppingCartIListVM
import com.zllkj.mm.module.shopping.vm.OrderDetailActivityVM
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.activity_order_detail.*
import kotlinx.android.synthetic.main.fragment_goods_detail.*
import kotlinx.android.synthetic.main.fragment_goods_detail.view.*
import kotlinx.android.synthetic.main.include_action_bar.*
import kotlinx.android.synthetic.main.layout_topbar.view.*
import java.math.BigDecimal
import java.util.HashMap

class GoodsDetailFragment(val goodsId:String) : BaseTitleFragment() {
    var vm = GoodsDetailFragmentVM()
    lateinit var curGoods : Goods
    override fun getContentLayoutId(): Int {
        return R.layout.fragment_goods_detail;
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var binding = DataBindingUtil.inflate<FragmentGoodsDetailBinding>(layoutInflater,R.layout.fragment_goods_detail,null,false)
        binding.goodsDetailFragmentVM = vm;
        mRootView = binding.root
        loadData()
        return mRootView
    }


    override fun loadData() {
        super.loadData()

        addSubscribe(RetrofitHelper.getInstance().goodsApi.getGoodsDetail((HashMap<String, Any>().apply {
            this["goodsId"] = goodsId
        })).compose<GoodsDetailVo>(RxHelper.defaultHandleResult<GoodsDetailVo>()).subscribe(object : ProgressSubscriber<GoodsDetailVo>(requireContext()){
            override fun onSucc(t: GoodsDetailVo?) {
                t?.let {
                    vm.goodsPicUrl.set(t.goodsPic)
                    vm.goodsName.set(t?.goodsName)
                    vm.goodsPrice.set(FormatUtil.bigDecimalUtilFormat(t.unitPrice.toString(),"100"))
                    vm.goodsShoppingWay.set(t.deliveryModeName)
                    vm.goodsCount.set("1")
                    vm.shopName = t?.shopName
                    vm.shopId = t?.shopId.toString()
                    vm.isCollate = t?.collected
                    curGoods = Goods(t?.goodsId,t?.goodsName,1,t?.goodsPic,-1,-1,t?.unitPrice)
                    if (t?.collected == 0) mRootView.iv_good_detail_collection_icon.setImageResource(R.mipmap.ic_collection_false) else mRootView.iv_good_detail_collection_icon.setImageResource(R.mipmap.ic_goods_collection_true)
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

        mRootView.tv_goods_detail_add_to_shopping_cart.setOnClickListener { addToShoppingCart() }
        mRootView.ll_collection_area.setOnClickListener { collectGoods() }
        mRootView.tv_goods_detail_go_shopping_cart.setOnClickListener { goToShoppingCartList() }
        mRootView.tv_goods_detail_go_shop_detail.setOnClickListener { goToShopDetail() }
        mRootView.tv_goods_detail_go_staff.setOnClickListener { L.showToast("尽请期待") }
        mRootView.tv_goods_detail_buy_now.setOnClickListener { buyNow() }
    }

    fun goToShoppingCartList(){
        addFragment(ShoppingCardFragment())
    }

    fun buyNow(){
        var itemShoppingCartIListVM = ItemShoppingCartIListVM()
        val datas = ArrayList<ItemShoppingCartIListVM>()
        itemShoppingCartIListVM.shopId = vm.shopId
        itemShoppingCartIListVM.shopName = vm.shopName
        itemShoppingCartIListVM.goodsList = ArrayList<Goods>().apply { add(curGoods) }
        datas.add(itemShoppingCartIListVM)
        var f = OrderConfirmFragment(datas);
        addFragment(f)
    }

    fun goToShopDetail(){

    }


    fun addToShoppingCart(){
        addSubscribe(RetrofitHelper.getInstance().shoppingCartApi.addGoodsToShoppingCart((HashMap<String, Any>().apply {
            this["goodsId"] = goodsId
            this["goodsNum"] = 1
        })).compose<AddShoppingCartVo>(RxHelper.defaultHandleResult<AddShoppingCartVo>()).subscribe(object : ProgressSubscriber<AddShoppingCartVo>(requireContext()) {
            override fun onSucc(t: AddShoppingCartVo?) {
                L.showToast("添加成功")
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
                L.showToast("添加失败")
            }

        }))
    }

    fun collectGoods(){
        if(vm.isCollate == 0) {
            addSubscribe(RetrofitHelper.getInstance().goodsApi.addGoodsCollection((HashMap<String, Any>().apply {
                this["goodsId"] = goodsId
            })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(requireContext()){
                override fun onSucc(t: String?) {
                    mRootView.iv_good_detail_collection_icon.setImageResource(R.mipmap.ic_goods_collection_true)
                    vm.isCollate = 1
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                }

            }))
        } else {
            addSubscribe(RetrofitHelper.getInstance().goodsApi.cancelGoodsCollection((HashMap<String, Any>().apply {
                this["goodsId"] = goodsId
            })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(requireContext()){
                override fun onSucc(t: String?) {
                    mRootView.iv_good_detail_collection_icon.setImageResource(R.mipmap.ic_collection_false)
                    vm.isCollate = 0
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                }

            }))
        }


    }



}